package org.dromara.app.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.bean.domain.bo.HisMemberBo;
import org.dromara.bean.domain.vo.HisMemberVo;
import org.dromara.bean.domain.vo.MemberInfoVo;
import org.dromara.bean.service.IHisMemberService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 患者
 *
 * @author Lion Li
 * @date 2024-11-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/member")
public class AHisMemberController extends BaseController {

    private final IHisMemberService hisMemberService;




    /**
     * 获取患者详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<HisMemberVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(hisMemberService.queryById(id));
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @SaIgnore
    @GetMapping("/getInfo")
    public R<MemberInfoVo> getInfo() {
        MemberInfoVo userInfoVo = new MemberInfoVo();
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (TenantHelper.isEnable() && LoginHelper.isSuperAdmin()) {
            // 超级管理员 如果重新加载用户信息需清除动态租户
            TenantHelper.clearDynamic();
        }
        HisMemberVo user = hisMemberService.queryById(loginUser.getUserId());
        if (ObjectUtil.isNull(user)) {
            return R.fail("没有权限访问用户数据!");
        }
        userInfoVo.setMember(user);
        return R.ok(userInfoVo);
    }

    /**
     * 修改患者
     */
    @Log(title = "患者", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HisMemberBo bo) {
        return toAjax(hisMemberService.updateByBo(bo));
    }

    /**
     * 删除患者
     *
     * @param ids 主键串
     */
    @Log(title = "患者", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(hisMemberService.deleteWithValidByIds(List.of(ids), true));
    }
}
